/********************************************************************
* @Copyright: Metanergy Technology R&D Co., Ltd
* @Filename: myg0025_pwr.c
* @brief  :
* @Author : AE Team
* @Version: V1.0.0/2023-04-12

********************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "myg0025_pwr.h"
#include "myg0025_rcc.h"

/** @addtogroup myg0025_StdPeriph_Driver
  * @{
  */

/** @defgroup PWR  PWR
  * @brief PWR driver modules
  * @{
  */

/** @defgroup PWR_Private_Defines PWR_Private_Defines
  * @{

@verbatim
*/

/* CR register bit mask */
#define CR_DS_MASK               ((uint32_t)0xFFFFFFFE)

/**
  * @brief  Deinitializes the PWR peripheral registers to their default reset values.
  * @param  None
  * @retval None
  */


void PWR_DeInit(void)
{
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE);
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, DISABLE);
}




/** @defgroup PWR_Group Low Power modes configuration functions
 *  @brief   Low Power modes configuration functions
 *
@verbatim
  ==============================================================================
              ##### Low Power modes configuration functions #####
  ==============================================================================

    [..] The devices feature one low-power modes:
    (+) Stop mode: all clocks are stopped, regulator running, regulator in low power mode

  *** Stop mode ***
  =================
  [..] In Stop mode, all clocks in the VCORE domain are stopped, the PLL, the HSI,
        and the HSE RC oscillators are disabled. Internal SRAM and register
       contents are preserved.

    (+) Entry:
        (++) The Stop mode is entered using the PWR_EnterSTOPMode(PWR_Regulator_LowPower,)
    (+) Exit:
        (++) Any EXTI Line (Internal or External) configured in Interrupt/Event mode
             or any internal IPs (I2C, UASRT or CEC) wakeup event.


@endverbatim
  * @{
  */


/**
  * @}
  */

/**
  * @brief  Enters STOP mode.
  * @note   In Stop mode, all I/O pins keep the same state as in Run mode.
  * @note   When exiting Stop mode by issuing an interrupt or a wakeup event,
  *         the HSI RC oscillator is selected as system clock.
  *         By keeping the internal regulator ON during Stop mode, the consumption
  *         is higher although the startup time is reduced.
  * @param  PWR_Regulator: Turn off the internal LDO option.
  *             @arg PWR_Regulator_ON      :Internal LDO open.
  *             @arg PWR_Regulator_LowPower: Internal LDO closed.
  * @param  PWR_Entry: specifies if STOP mode in entered with WFI or WFE instruction.
  *         This parameter can be one of the following values:
  *             @arg PWR_STOPEntry_WFI: enter STOP mode with WFI instruction
  *             @arg PWR_STOPEntry_WFE: enter STOP mode with WFE instruction
                @arg PWR_STOPEntry_SLEEPONEXIT: enter STOP mode with SLEEPONEXIT instruction
  * @retval None
  */


void PWR_EnterStopMode(uint32_t PWR_Regulator, uint8_t PWR_Entry)
{
    uint32_t tmpreg = 0;
    /* Check the parameters */
    assert_param(IS_PWR_REGULATOR(PWR_Regulator));
    assert_param(IS_PWR_ENTRY(PWR_Entry));

    /* Select the regulator state in Stop mode ---------------------------------*/
    tmpreg = PWR->CR;
    /* Clear PDDS and LPDS bits */
    tmpreg &= CR_DS_MASK;
    /* Set LPDS bit according to PWR_Regulator value */
    tmpreg |= PWR_Regulator;
    /* Store the new value */
    PWR->CR = tmpreg;
    /* Set SLEEPDEEP bit of Cortex System Control Register */
    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;

    /* Select Stop mode entry --------------------------------------------------*/
    if (PWR_Entry == PWR_Entry_WFI)
    {
        /* Request Wait For Interrupt */
        __WFI();


    }
    else if (PWR_Entry == PWR_Entry_WFE)
    {
        /* Request Wait For Event */
        __SEV();
        __WFE();
        __WFE();
    }
    else
    {
        /* Set SLEEP on exit bit of Cortex-M0 System Control Register */
        SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk;
    }

    /* Reset SLEEPDEEP bit of Cortex System Control Register */
    SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
}


void PWR_EnterSleepMode(uint8_t PWR_SLEEPEntry)
{
    /* Check the parameters */
    assert_param(IS_PWR_SLEEP_ENTRY(PWR_SLEEPEntry));

    /* Clear SLEEPDEEP bit of Cortex-M0 System Control Register */
    SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);

    /* Select SLEEP mode entry -------------------------------------------------*/
    if (PWR_SLEEPEntry == PWR_SLEEPEntry_WFI)
    {
        /* Request Wait For Interrupt */
        __WFI();
    }
    else
    {
        /* Request Wait For Event */
        __SEV();
        __WFE();
        __WFE();
    }
}



/** @defgroup PWR_Group3 Flags management functions
 *  @brief   Flags management functions
 *
@verbatim
  ==============================================================================
                       ##### Flags management functions #####
  ==============================================================================

@endverbatim
  * @{
  */

/**
  * @brief  Checks whether the specified PWR flag is set or not.
  * @param  PWR_FLAG: specifies the flag to check.
  *          This parameter can be one of the following values:
  *             @arg PWR_FLAG_PVD_INT: PVD Output. This flag is valid only if PVD
  *                  is enabled by the PWR_PVDCmd() function.
  *             @arg PWR_FLAG_VREFINTRDY: Internal Voltage Reference Ready flag.
  *                  This flag indicates the state of the internal voltage
  *                  reference, VREFINT.
  * @retval The new state of PWR_FLAG (SET or RESET).
  */



FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG)
{

    FlagStatus bitstatus = RESET;

    /* Check the parameters */

    assert_param(IS_PWR_GET_FLAG(PWR_FLAG));


    if ((PWR->CR & PWR_FLAG) != (uint32_t)RESET)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }
    /* Return the flag status */
    return bitstatus;
}





/**
  * @brief  Clears the PWR's pending flags.
  * @param  PWR_FLAG: specifies the flag to clear.
  *          This parameter can be one of the following values:
  *             @arg PWR_FLAG_WU: Wake Up flag
  *             @arg PWR_FLAG_SB: StandBy flag
  * @retval None
  */
void PWR_ClearFlag(uint32_t PWR_FLAG)
{

    /* Check the parameters */

    assert_param(IS_PWR_GET_FLAG(PWR_FLAG));

    PWR->CR |=  PWR_FLAG << 2;

}




/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */
